import 'package:flutter/material.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:syria_sports/presentation/sync_screen/sync_tabs/field_operations_tab.dart';
import 'package:syria_sports/presentation/sync_screen/sync_tabs/pending_operations_tab.dart';
import 'package:syria_sports/presentation/sync_screen/sync_tabs/success_operations_tab.dart';
import 'cubit/sync_bloc.dart';


class SyncScreen extends StatelessWidget {
  const SyncScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: BlocProvider(
        create: (_) => SyncBloc(SyncState())..add(SyncInitialEvent()),
        child: BlocBuilder<SyncBloc, SyncState>(
          builder: (context, state) {
            print("state is $state");
            var cubit = SyncBloc.get(context);
            return Scaffold(
              appBar: AppBar(
                title: const Text('عمليات المزامنة'),
                bottom: TabBar(
                  unselectedLabelStyle: theme.textTheme.labelLarge,
                  labelStyle: theme.textTheme.titleLarge?.copyWith(color: theme.colorScheme.onPrimary),
                  tabs: [
                    Tab(text: 'معلقة (${state.pending.length})'), // العمليات المعلقة
                    Tab(text: 'ناجحة (${state.success.length})'), // العمليات المزامنة
                    Tab(text: 'فشلت (${state.field.length})'), // العمليات الفاشلة
                  ],
                  onTap: (value){
                    if(value == 0){
                      cubit.add(FetchPOperationsEvent());
                    }else if(value == 1){
                      cubit.add(FetchSOperationsEvent());
                    }else{
                      cubit.add(FetchFOperationsEvent());
                    }
                  },
                ),
              ),
              body: Column(
                children: [
                  Expanded(
                    child: TabBarView(
                      children: [
                        PendingOperationsTab(
                          operations:state.pending
                        ),
                        SuccessOperationsTab(
                          operations: state.success,
                        ),
                        FieldOperationsTab(
                          operations: state.field,
                        ),
                      ],
                    ),
                  ),

                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
